package com.wanlian.fileserver.device.worker;


import com.wanlian.netty.File.msg.FileBaseMsg;
import com.wanlian.netty.File.msg.FilePeerMsg;
import com.wanlian.netty.File.msg.FilePeerMsgQueue;
import com.wanlian.netty.File.msg.down.FilePeerDownCheckMsg;
import com.wanlian.netty.File.msg.up.FilePeerUpCheckMsg;
import com.wanlian.netty.model.ResultInfo;
import com.wanlian.netty.model.Session;
import com.wanlian.netty.model.SessionPool;
import com.wanlian.netty.msg.MsgType;
import io.netty.channel.Channel;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DeviceServerFileWorker {

    public static SessionPool sessionPool = new SessionPool();

    /**
     * 工作线程
     */
    public static void work() {
        calcPeerMsg();
    }

    /**
     * 发送数据给某个节点
     *
     * @param key
     * @param fileBaseMsg
     */
    public static ResultInfo sendDataByKey(String key, FileBaseMsg fileBaseMsg) {
        if (sessionPool.isExist(key)) {
            Session session = sessionPool.getSessionByPublicKey(key);
            FilePeerMsg filePeerMsg = new FilePeerMsg(fileBaseMsg);
            Channel channel = session.getChannel();
            channel.writeAndFlush(filePeerMsg);
            return new ResultInfo(true);
        } else {
            return new ResultInfo(false, "终端不存在或已经掉线");
        }
    }


    /**
     * 处理网络消息
     */
    private static void calcPeerMsg() {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            fixedThreadPool.execute(new Runnable() {
                public void run() {
                    while (true) {
                        try {
                            TimeUnit.MILLISECONDS.sleep(100);
                            processFileMsg();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }


    private static synchronized void processFileMsg() {
        try {
            List<FilePeerMsg> filePeerMsgList = FilePeerMsgQueue.get(100);
            for (FilePeerMsg item : filePeerMsgList) {
                FileBaseMsg baseMsg = item.getFileBaseMsg();
                String msgType = baseMsg.getType();
                String tagClientId = baseMsg.getBaseHead().getTagClientId();
                //上传
                if (msgType.equals(MsgType.DEVICE_FILE_UPLOAD_HEAD)) {
                    replyUpload(baseMsg);
                } else if (msgType.equals(MsgType.DEVICE_FILE_UPLOAD_REQ)) {
                    replyUpload(baseMsg);
                } else if (msgType.equals(MsgType.DEVICE_FILE_UPLOAD_RSP)) {
                    replyUpload(baseMsg);
                } else if (msgType.equals(MsgType.DEVICE_FILE_UPLOAD_CHECK)) {
                    sendDataByKey(tagClientId, baseMsg);
                } else if (msgType.equals(MsgType.DEVICE_FILE_UPLOADCheck_REQ)) {
                    sendDataByKey(tagClientId, baseMsg);
                }
                //下载
                else if (msgType.equals(MsgType.DEVICE_FILE_DOWNLOAD_HEAD)) {
                    replyDown(baseMsg);
                } else if (msgType.equals(MsgType.DEVICE_FILE_DOWNLOAD_CHECK)) {
                    sendDataByKey(tagClientId, baseMsg);
                } else if (msgType.equals(MsgType.DEVICE_FILE_DOWNLOAD_REQ)) {
                    replyDown(baseMsg);
                } else if (msgType.equals(MsgType.DEVICE_FILE_DOWNLOAD_RSP)) {
                    replyDown(baseMsg);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void replyUpload(FileBaseMsg baseMsg) {
        String tagClientId = baseMsg.getBaseHead().getTagClientId();
        String clientId = baseMsg.getBaseHead().getClientId();
        ResultInfo resultInfo = sendDataByKey(tagClientId, baseMsg);
        if (!resultInfo.isBresult()) {
            FilePeerUpCheckMsg filePeerUpCheckMsg = new FilePeerUpCheckMsg();
            filePeerUpCheckMsg.getBaseHead().setClientId(tagClientId);
            filePeerUpCheckMsg.getBaseHead().setTagClientId(clientId);
            filePeerUpCheckMsg.setResultInfo(resultInfo);
            sendDataByKey(clientId, filePeerUpCheckMsg);
        }
    }

    private static void replyDown(FileBaseMsg baseMsg) {
        String tagClientId = baseMsg.getBaseHead().getTagClientId();
        String clientId = baseMsg.getBaseHead().getClientId();
        ResultInfo resultInfo = sendDataByKey(tagClientId, baseMsg);
        if (!resultInfo.isBresult()) {
            FilePeerDownCheckMsg filePeerDownCheckMsg = new FilePeerDownCheckMsg();
            filePeerDownCheckMsg.getBaseHead().setClientId(tagClientId);
            filePeerDownCheckMsg.getBaseHead().setTagClientId(clientId);
            filePeerDownCheckMsg.setResultInfo(resultInfo);
            sendDataByKey(clientId, filePeerDownCheckMsg);
        }
    }

}

